package com.health.system.service.impl;

import java.util.HashMap;
import java.util.List;
import com.health.common.utils.DateUtils;
import com.health.system.domain.MallOrderCloseRequest;
import com.health.system.domain.vo.OrderDetailVO;
import com.health.system.domain.vo.OrderStatusCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.health.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.health.system.domain.MallAfterSale;
import com.health.system.mapper.MallOrderMapper;
import com.health.system.domain.MallOrder;
import com.health.system.service.IMallOrderService;

/**
 * 订单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-29
 */
@Service
public class MallOrderServiceImpl implements IMallOrderService 
{
    @Autowired
    private MallOrderMapper mallOrderMapper;

    /**
     * 查询订单管理
     * 
     * @param orderId 订单管理主键
     * @return 订单管理
     */
    @Cacheable(value = "order" , key = "#orderId")
    @Override
    public MallOrder selectMallOrderByOrderId(Long orderId)
    {
        return mallOrderMapper.selectMallOrderByOrderId(orderId);
    }

    /**
     * 查询订单管理列表
     * 
     * @param mallOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<MallOrder> selectMallOrderList(MallOrder mallOrder)
    {
        return mallOrderMapper.selectMallOrderList(mallOrder);
    }

    /**
     * 新增订单管理
     * 
     * @param mallOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMallOrder(MallOrder mallOrder)
    {
        mallOrder.setCreateTime(DateUtils.getNowDate());
        int rows = mallOrderMapper.insertMallOrder(mallOrder);
        insertMallAfterSale(mallOrder);
        return rows;
    }

    /**
     * 修改订单管理
     * 
     * @param mallOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMallOrder(MallOrder mallOrder)
    {
        mallOrder.setUpdateTime(DateUtils.getNowDate());
        mallOrderMapper.deleteMallAfterSaleByOrderId(mallOrder.getOrderId());
        insertMallAfterSale(mallOrder);
        return mallOrderMapper.updateMallOrder(mallOrder);
    }

    /**
     * 批量删除订单管理
     * 
     * @param orderIds 需要删除的订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMallOrderByOrderIds(Long[] orderIds)
    {
        mallOrderMapper.deleteMallAfterSaleByOrderIds(orderIds);
        return mallOrderMapper.deleteMallOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单管理信息
     * 
     * @param orderId 订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMallOrderByOrderId(Long orderId)
    {
        mallOrderMapper.deleteMallAfterSaleByOrderId(orderId);
        return mallOrderMapper.deleteMallOrderByOrderId(orderId);
    }

    /**
     * 新增售后管理信息
     * 
     * @param mallOrder 订单管理对象
     */
    public void insertMallAfterSale(MallOrder mallOrder)
    {
        List<MallAfterSale> mallAfterSaleList = mallOrder.getMallAfterSaleList();
        Long orderId = mallOrder.getOrderId();
        if (StringUtils.isNotNull(mallAfterSaleList))
        {
            List<MallAfterSale> list = new ArrayList<MallAfterSale>();
            for (MallAfterSale mallAfterSale : mallAfterSaleList)
            {
                mallAfterSale.setOrderId(orderId);
                list.add(mallAfterSale);
            }
            if (list.size() > 0)
            {
                mallOrderMapper.batchMallAfterSale(list);
            }
        }
    }
    /**
     * 根据订单状态统计订单数量
     *
     * @return 订单数量
     */
    @Override
    public Map<String, Integer> getOrderCountByStatus(MallOrder mallOrder) {
        List<OrderStatusCount> resultList = mallOrderMapper.getOrderStatusCount(mallOrder);
        //转换为Map<String, Integer>
        Map<String, Integer> statusCountMap = new HashMap<>();
        for (OrderStatusCount result : resultList) {
            if (result.getStatusCode() != null && result.getCount() != null){
                statusCountMap.put(result.getStatusCode(),result.getCount());
            }

        }
        return statusCountMap;
    }
    /**
     * 关闭订单
     */
    @Override
    public int closeOrder(MallOrderCloseRequest closeRequest) {
        MallOrder mallOrder = mallOrderMapper.selectMallOrderByOrderId(closeRequest.getOrderId());
        if (mallOrder == null) {
            return 0;
        }
        // 如果有关闭原因，则更新备注（可选）
        if (closeRequest.getCloseReason() != null && !closeRequest.getCloseReason().trim().isEmpty()) {
            // 直接将关闭原因设置为备注（或者追加到原有备注）
            String newRemark = closeRequest.getCloseReason();
            mallOrder.setRemark(newRemark);
        }
        // 设置订单状态为已关闭（使用状态码4）
        mallOrder.setStatus("4");
        mallOrder.setUpdateTime(DateUtils.getNowDate());

        return mallOrderMapper.updateMallOrder(mallOrder);
    }

    @Override
    public OrderDetailVO getOrderDetail(Long orderId) {
        return mallOrderMapper.selectOrderDetailByOrderId(orderId);
    }


}
